/**
 * new $.fn.dataTable.Editor( {
 *   "ajax": "php/dates.php",
 *   "table": "#example",
 *   "fields": [ {
 *       "label": "Notes:",
 *       "name": "notes",
 *       "type": "ueditor",
 *       "opts": {
 *         toolbars : [['anchor']],
 *         // additional options if required...
 *       }
 *     }, 
 *     // additional fields...
 *   ]
 * } );
 */

(function(factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery', 'datatables', 'datatables-editor'], factory);
    }
    else if (typeof exports === 'object') {
        // Node / CommonJS
        module.exports = function($, dt) {
            if (!$) {
                $ = require('jquery');
            }
            factory($, dt || $.fn.dataTable || require('datatables'));
        };
    }
    else if (jQuery) {
        // Browser standard
        factory(jQuery, jQuery.fn.dataTable);
    }
}(function($, DataTable) {
    'use strict';


    if (!DataTable.ext.editorFields) {
        DataTable.ext.editorFields = {};
    }

    var _fieldTypes = DataTable.Editor ?
            DataTable.Editor.fieldTypes :
            DataTable.ext.editorFields;


    _fieldTypes.ueditor = {
        create: function(conf) {
            var that = this;
            conf._safeId = DataTable.Editor.safeId(conf.id);
            
            conf._input = $('<div><textarea id="' + conf._safeId + '" style="height:300px;"></textarea></div>');
            this
                .on('open.ueditorInit-' + conf._safeId, function() {
                    var options = $.extend(true, {
                        //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个
                        toolbars:[
                            [

                                'fullscreen', //全屏

                            ]
                        ],
                        //关闭elementPath
                        elementPathEnabled:false,
                        lang:'zh-cn',
                        //默认的编辑区域高度
                        initialFrameHeight:300
                    }, conf.opts);
                    
                    var editor = UE.getEditor(conf._safeId, options);
                    editor.addListener( 'ready', function( editor ) {
                        $("#" + conf._safeId).removeClass("form-control");
                        if (editor && conf._initSetVal) {
                            UE.getEditor(conf._safeId).setContent(conf._initSetVal, false);
                            conf._initSetVal = null;
                        }
                    });
                })
                .on('close.ueditorInit-' + conf._safeId, function() {
                    var editor = UE.getEditor(conf._safeId);
                    if (editor) {
                        editor.destroy();
                    }
                    
                    conf._initSetVal = null;
                    conf._input.find('textarea').val('');
                });
            return conf._input;
        },
        get: function(conf) {
            if ($("#" + conf._safeId).size() > 0) {
                return UE.getEditor(conf._safeId).getContent();
            }
            return conf._initSetVal;
        },
        set: function(conf, val) {
            conf._initSetVal = val;
            if ($("#" + conf._safeId).size() > 0) {
                return UE.getEditor(conf._safeId).setContent(val);
            }
        },
        enable: function(conf) {
            if ($("#" + conf._safeId).size() > 0) {
                UE.getEditor(conf._safeId).setEnabled();
            }
        },

        disable: function(conf) {
            if ($("#" + conf._safeId).size() > 0) {
                UE.getEditor(conf._safeId).setDisabled('fullscreen');
            }
        },
        ueditor: function(conf) {
            return UE.getEditor(conf._safeId);
        }

    };


}));
